package com.tanhua.dubbo.api.db;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.api.mapper.ReportMapper;
import com.tanhua.model.db.Report;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Version 1.0
 * @Author aao
 * @Date 2022/2/21 16:50
 * @Description 问卷报告服务接口实现类
 */
@DubboService
public class ReportApiImpl implements ReportApi{

    @Autowired
    private ReportMapper reportMapper;
    /**
     * 根据问卷id和用户id查询报告
     * @param questionnaireId
     * @param userId
     * @return
     */
    @Override
    public Report findByQuestionnaireIdAndUserId(Long questionnaireId, Long userId) {
        LambdaQueryWrapper<Report> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Report::getUserId,userId)
                .eq(Report::getQuestionnaireId,questionnaireId);
        return reportMapper.selectOne(queryWrapper);
    }

    /**
     * 查询报告
     * @param reportId
     * @return
     */
    @Override
    public Report findById(Long reportId) {
        return reportMapper.selectById(reportId);
    }

    /**
     * 查询报告表 获取与你相似用户id
     * @param report
     * @return
     */
    @Override
    public List<Long> findSimilarYouIds(Report report) {
        LambdaQueryWrapper<Report> queryWrapper = new LambdaQueryWrapper<>();
        //设置条件 1.问卷id相同 2.人格类型相同 3.性别不同 4.非当前用户
        //5.四个维度项差值不超过10(between) 6.获取所有符合条件的报告集合，通过流转化成userId集合 并随机取10条返回
        queryWrapper.eq(Report::getQuestionnaireId,report.getQuestionnaireId())
                .eq(Report::getCharacterType,report.getCharacterType())
                .ne(Report::getGender,report.getGender())
                .ne(Report::getUserId,report.getUserId())
                .between(Report::getOutgoing,report.getOutgoing() - 10,report.getOutgoing() + 10)
                .between(Report::getJudge,report.getJudge() - 10,report.getJudge() + 10)
                .between(Report::getRationality,report.getRationality() - 10,report.getRationality() + 10)
                .between(Report::getAbstraction,report.getAbstraction() - 10,report.getAbstraction() + 10);

        List<Report> reportList = reportMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(reportList)) {
            return null;
        }
        List<Long> idList = reportList.stream().map(Report::getUserId).collect(Collectors.toList());
        idList = RandomUtil.randomEles(idList, 10);

        return idList;
    }

    /**
     * 新增/更新报告
     *
     * @param report
     */
    @Override
    public Long add(Report report) {
        LambdaQueryWrapper<Report> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Report::getUserId, report.getUserId()).eq(Report::getQuestionnaireId, report.getQuestionnaireId());
        Report reportInDB = reportMapper.selectOne(lqw);
        Long reportId;
        if (reportInDB != null) {
            reportId = reportInDB.getId();
            reportMapper.updateById(report);
        } else {
            reportMapper.insert(report);
            reportId = report.getId();
        }
        return reportId;
    }
}
